package org.advanced.rpc;

import org.advanced.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author John.zhang
 * @version Id: RemoteInvocationHandler, v 1.0 2020/12/14 18:38 ZhiYong Exp $
 */
public class RemoteInvocationHandler implements InvocationHandler {

    private String host;

    private int port;

    public RemoteInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RpcRequest request = new RpcRequest();
        request.setClassName(method.getDeclaringClass().getName());
        request.setMethodName(method.getName());
        request.setParameters(args);
        request.setTypes(method.getParameterTypes());
        RpcNetTransPort rpcNetTransPort = new RpcNetTransPort(host,port);
        Object result = rpcNetTransPort.send(request);
        return result;
    }
}
